package yl.hs.bmipfull.service.laboratorydata.imple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratorydata.ILabExperimentItemsDao;
import yl.hs.bmipfull.pojo.laboratory.PackageInfoEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabExperimentDefinitionlibsEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabExperimentItemsEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabExperimentItemsService;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:   <br/>
 * 创建时间: 2022-06-20 10:48 <br/>
 *
 * @author 吴高丰
 */
@Service
public class LaboratorydataServerImple extends LaboratorydataBaseService implements ILabExperimentItemsService {
    @Autowired
    ILabExperimentItemsDao dao;
    @Override
    public List<LabExperimentItemsEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabExperimentItemsEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabExperimentItemsEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabExperimentItemsEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabExperimentItemsEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabExperimentItemsEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabExperimentItemsEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }

    @Override
    public LabExperimentItemsEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public Integer saveLabExperimentItems(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabExperimentItemsEntity entity = new LabExperimentItemsEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabExperimentItemsEntity.class));
            code.clear();
            var parecode = getParentCode(entity.getParentSN());
            parecode.add(entity.getSn());
            entity.setParentCode(String.join(",",parecode));
            entity.setLayer(parecode.size()-1);
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), changelog, Tl.toJson(entity), PackageInfoEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新标检测项信息", Tl.toJson(entity), PackageInfoEntity.class);
            }
        }
        return result;
    }

    @Override
    public List<Object> queryByDefinitionSN(String definitionSN) {
       var rsult=  dao.queryListByDefinitionSN(definitionSN);
        List<Object> result= new ArrayList<>();
        rsult.forEach(t->{
            var entity = t.toMap();
            if(!t.getParentSN().isEmpty()) {
                entity.put("parentName", dao.findBySN(t.parentSN).getName());
            }else{
                entity.put("parentName", null);
            }
            result.add(entity);
        });
        return result;
    }

    @Override
    public List<LabExperimentItemsEntity> queryByParentSN(String parentSN) {
        return dao.queryByParentSN(parentSN);
    }

    @Override
    public Integer deleteBySn(String sn) {
        var item =  dao.queryByParentSN(sn);
        if(item!=null && item.size()>0){
            item.forEach(t->{
                var result =  dao.deleteBySn(sn);
                dao.deleteBySn(t.sn);
                deleteBySn(t.sn);
            });
        }else{
            dao.deleteBySn(sn);
        }
        return 1;
    }
    List<String> code= new ArrayList<>();
    public List<String> getParentCode(String sn){
        var result =  dao.findBySN(sn);
        if(result!=null){
            code.add(result.getSn());
            getParentCode(result.getParentSN());
        }
        return code;
    }
    public Map<String,Object> GetTree(){

        return null;
    }
}
